package service;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;

import model.ImageModel;
import entity.NewsImage;

@SuppressWarnings("unused")
public class ImageService {
	private static ImageModel imgModel = new ImageModel();

	public static void getImageFromUrl(String imgURL) {
		InputStream in = null;
		FileOutputStream fileOut = null;
		OutputStream out = null;
		try {
			System.out.println("Start getting image from url : " + imgURL);
			NewsImage img = imgModel.findByURL(imgURL);
			if (img.isOk()) {
				// make folder in webapp if it's not exits : root folder, folder
				// by year, by month, by day.
				File f = new File("test.txt");
				String path = f.getAbsolutePath();
				System.out.println("Root path : " + path);
				File superFolder = null;
				if (path.contains("root")) {
					superFolder = new File(
							"apache-tomcat-7.0.22/webapps/images_news");
				} else {
					superFolder = new File("../webapps/images_news");
				}

				if (!superFolder.exists()) {
					superFolder.mkdir();
					System.out
							.println("Create new image_news folder success !");
				}

				File yearFolder = new File(superFolder, img.getYear());
				if (!yearFolder.exists() && superFolder.exists()) {
					yearFolder.mkdir();
					System.out.println("Create new year folder success !");
				}

				File monthFolder = new File(yearFolder, img.getMonth());
				if (!monthFolder.exists() && yearFolder.exists()) {
					monthFolder.mkdir();
					System.out.println("Create new month folder success !");
				}

				File dayFolder = new File(monthFolder, img.getDay());
				if (!dayFolder.exists() && monthFolder.exists()) {
					dayFolder.mkdir();
					System.out.println("Create new day folder success !");
				}

				// make image.
				File myImg = new File(dayFolder, img.getName());
				if (!myImg.exists() && dayFolder.exists()) {
					myImg.createNewFile();
					System.out
							.println("Create image success. Getting image content.");
				}

				// get image from url and write to file.
				if (myImg.exists()) {
					URL url = new URL(img.getUrl());
					URLConnection uc = url.openConnection();
					in = uc.getInputStream();
					fileOut = new FileOutputStream(myImg);
					out = new BufferedOutputStream(fileOut);

					// init buffer based on internet bandwidth.
					final byte[] buf = new byte[1024 * 1024];
					for (int count = in.read(buf); count != -1; count = in
							.read(buf)) {
						out.write(buf, 0, count);
					}

					imgModel.delete(img);
					System.out.println("Download image complete !");
				} else {
					System.out
							.println("Cannot create image file. Error occur.");
				}

			}
		} catch (Exception ex) {
			System.out.println("Error when getting image : ");
			ex.printStackTrace();
		} finally {
			try {
				// close all in, out stream.
				out.flush();
				out.close();
				fileOut.flush();
				fileOut.close();
				in.close();
			} catch (IOException ex) {
				System.out.println("Error when close image stream : ");
				ex.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {
		String a = "/test.txt";
		if(a.contains("root")){
			System.out.println("Fuck");
		}
		System.out.println(a.split("/")[1]);
		// String url =
		// "http://www.earthsbeauty.com/Nics%20Beauty%20Earths%20Beauty%20.jpg";
		// Calendar cal = Calendar.getInstance();
		// int day = cal.get(Calendar.DATE);
		// int month = cal.get(Calendar.MONTH);
		// int year = cal.get(Calendar.YEAR);
		// NewsImage img = new NewsImage();
		// img.setUrl(url);
		// img.setDay("" + day);
		// img.setMonth("" + (month + 1));
		// img.setYear("" + year);
		// img.setStatus(1);
		// img.setName("example.jpg");
		// ImageModel model = new ImageModel();
		// model.save(img);
		// ImageService.getImageFromUrl(img.getUrl());
	}
}
